package visuals;

import options.Options;
import song.TimeSignature;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: Aleksey
 * Date: 03.01.2008
 * Time: 1:50:47
 */
public class EastPanel extends JPanel {
    JLabel currentBar = new JLabel("120 of 130");
    JLabel timeSignature = new JLabel("Time Signature:");
    JLabel slash = new JLabel(" / ");
    JTextField beatNumber = new JTextField("40");
    JTextField beatUnit = new JTextField("40");
    JPanel timeSig = new JPanel();
    JPanel tempoPanel = new JPanel();
    JTextField tempoField = new JTextField("20000");
    JLabel tempo = new JLabel("Tempo:");
    JPanel components = new JPanel();
    JButton apply = new JButton("Apply");
    JLabel songNameLabel = new JLabel("Song name:");
    JTextField songName = new JTextField("song1");

    public JTextField getTempoField() {
        return tempoField;
    }

    public JTextField getBeatUnit() {
        return beatUnit;
    }

    public JTextField getBeatNumber() {
        return beatNumber;
    }

    public JLabel getCurrentBar() {
        return currentBar;
    }

    public JTextField getSongName() {
        return songName;
    }

    public EastPanel() {
        setLayout(new BorderLayout());
        components.setLayout(new GridLayout(20, 1, 10, 10));
        currentBar.setPreferredSize(new Dimension(140, 18));
        currentBar.setFont(new Font("Verdana", Font.BOLD, 23));
        currentBar.setForeground(Color.RED);
        components.add(currentBar);
        components.add(timeSignature);
        beatNumber.setPreferredSize(new Dimension(30, 18));
        timeSig.add(beatNumber);
        timeSig.add(slash);
        beatUnit.setPreferredSize(new Dimension(30, 18));
        timeSig.add(beatUnit);
        components.add(timeSig);
        components.add(tempo);
        tempoField.setPreferredSize(new Dimension(50, 18));
        tempoPanel.add(tempoField);
        components.add(tempoPanel);
        components.add(apply);
        components.add(songNameLabel);

        int i = 0;
        while (true) {
            if (!(new File(Options.DEFAULT_PATH + "song" + i + ".mid").exists())) {
                break;
            }
            i++;
        }
        songName.setText("song" + i);

        components.add(songName);
        add(components, BorderLayout.CENTER);
        add(new JPanel(), BorderLayout.NORTH);
        add(new JPanel(), BorderLayout.WEST);
        add(new JPanel(), BorderLayout.EAST);

        apply.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Integer currentBar = MainFrame.getCenterPanel().getCurrentBar();

                Integer beatUnitVar;
                Integer beatNumberVar;
                Integer tempoVar;
                try {
                    tempoVar = Integer.parseInt(tempoField.getText());
                    beatNumberVar = Integer.parseInt(beatNumber.getText());
                    beatUnitVar = Integer.parseInt(beatUnit.getText());
                }
                catch (NumberFormatException e1) {
                    JOptionPane.showMessageDialog(null, "Bad tempo and/or time signature. Check your input.", "Input error", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                MainFrame.getSong().setTempo(currentBar, tempoVar);

                Integer beatUnit1 = 0;
                while (beatUnitVar % 2 == 0) {
                    beatUnit1++;
                    beatUnitVar /= 2;
                }
                if (beatUnitVar != 1) {
                    JOptionPane.showMessageDialog(null, "Bad tempo and/or time signature. Check your input.", "Input error", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if (!MainFrame.getSong().getBar(0, currentBar).getTimeSignature().getBeatNumber().equals(beatNumberVar) ||
                        !MainFrame.getSong().getBar(0, currentBar).getTimeSignature().getBeatUnit().equals(beatUnit1)) {
                    MainFrame.getSong().setTimeSignature(currentBar, new TimeSignature(false, beatNumberVar, beatUnit1));
                    MainFrame.getCenterPanel().setCurrentPart(0);
                }

                MainFrame.getCenterPanel().paintLayout();
                MainFrame.getCenterPanel().repaint();
                MainFrame.getCenterPanel().requestFocus();
            }
        });
    }
}
